package com.zkingsoft.actions.common;

import java.util.List;

import javax.enterprise.context.Dependent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BaseServices;
/**
 * @description 除了特殊的action一般业务action都继承这个action来实现
 * 基本增删改查功能
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
public abstract class BaseController {

	public <T> AjaxResult add(BaseServices<T> baseServices, T t, String name) {
		int i = baseServices.add(t);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, name + "添加成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "添加失败");
		}
	}

	public <T> AjaxResult modify(BaseServices<T> baseServices, T t, String name) {
		int i = baseServices.modify(t);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, name + "修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "修改失败");
		}
	}

	public <T> AjaxResult remove(BaseServices<T> baseServices, String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = baseServices.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "删除失败");
		}
	}

	public <T> AjaxResult showList(BaseServices<T> baseServices, T t, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<T> dataList = baseServices.findInPage(t, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, baseServices.findTotal(t));
		return result;
	}

	public <T> AjaxResult findByModel(BaseServices<T> baseServices, T t) {
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, baseServices.findByModel(t), 0);
		return result;
	}

	public <T> T findById(BaseServices<T> baseServices, Long id) {
		return baseServices.findById(id);
	}

	/**
	 * 本方法已经过时请使用新方法
	 * 
	 * @see getMe()
	 * @param request
	 */
	@Dependent
	public Users getMe(HttpServletRequest request) {
		return (Users) request.getSession().getAttribute("user");
	}

	/**
	 * 2016/6/2 xieguangya
	 * 
	 * @return
	 */
	public Users getMe() {
		return (Users) getRequest().getSession().getAttribute("user");
	}

	/**
	 * 2016/6/2
	 * 
	 * @author xieguangya
	 * @return getRequest
	 */
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取session
	 * @author Matrix-J 
	 * @return HttpSession
	 */
	public HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
	}

	/**
	 * 2016年6月15日 获取ServletContext
	 * 
	 * @author Matrix-J
	 * @return getServletContext
	 */
	public ServletContext getServletContext() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession()
				.getServletContext();
	}

}
